﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;


namespace TappUpsRefresh.Extras
{
//    public enum LevelStore
//{
//     One, Two, Three, Four, Five
//}
    public enum Gender
    {
        Male, Female 
    }


    public class LevelStore
    {
        public List<Levels> Store = new List<Levels>();
    }

    public class Score
    {
        public String Country  {get; set; }
        public String Name { get; set; }
        public String Difficulty { get; set; }
        public Levels level { get; set; }
        public Gender gender { get; set; }
        public int score {get; set; }
        public int LevelNo { get; set; }
        public String photoPath { get; set; }

        public Score()
        {
            Country = Profile.Retrieve() != null ? Profile.Retrieve().Country : "USA";
            Name = Profile.Retrieve() != null ? Profile.Retrieve().Name : "player";
            Difficulty = SettingsClass.Retrieve() != null ? (SettingsClass.Retrieve().easyMode ? "Easy" : "Difficult") : "Easy";
            level = new Levels();
            LevelNo = level.LevelNo;
            int score = 0;
        }


        public void Update()
        {
            LevelNo = level.LevelNo;
        }
        public static void Save(Score score)
        {
            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(Score));
            try
            {
                using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(score.Name + score.Difficulty + score.level.LevelNo + ".score", FileMode.OpenOrCreate, isf))
                    {
                        xmls.Serialize(isfs, score);
                        isfs.Close();
                        isfs.Dispose();
                    }
                }
            }
            catch (Exception e)
            { }
        }

        public static Score Retrieve()
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(Score));
                System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
                System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(Profile.Retrieve().Name + (SettingsClass.Retrieve().easyMode ? "Easy" : "Difficult") + SettingsClass.Retrieve().level.LevelNo + ".score", FileMode.Open, isf);
                Score sc = (Score)xmls.Deserialize(isfs);
                isfs.Close();
                isfs.Dispose();
                return sc;
            }
            catch (Exception e)
            {              
                return null;
            }
        }
    }

    public class SettingsClass
    {
        public String Name = "playerName";
        public bool score = true;
        public bool easyMode = true;
        public Levels level = new Levels();
        public bool ShowJoyStick = false;
        public static void Save(SettingsClass settings)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(SettingsClass));
                using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("Settings.pfk", FileMode.OpenOrCreate, isf))
                    {
                        xmls.Serialize(isfs, settings);
                        isfs.Close();
                        isfs.Dispose();
                    }
                }
            }
            catch (Exception e)
            { }
        }

        public static SettingsClass Retrieve()
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(SettingsClass));
                System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
                System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("Settings.pfk", FileMode.Open, isf);
                SettingsClass sett = (SettingsClass)xmls.Deserialize(isfs);
                isfs.Close();
                isfs.Dispose();
                return sett;
            }
            catch (Exception e)
            {
                return null;
            }
        }



        public bool mute { get; set; }

        //public double Volume { get; set; }
    }

    public class Profile
    {
        public int Score;
        public String Name;
        public String Country;
        public String Difficulty;
        public String ImageName;
        public static void Save(Profile profile)
        {
            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(Profile));
            try
            {
                using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(profile.Name + ".profile", FileMode.Create, isf))
                    {
                        xmls.Serialize(isfs, profile);
                        isfs.Close();
                        isfs.Dispose();
                    }
                }
            }
            catch (Exception e)
            { }
        }

        public static Profile Retrieve()
        {
            try
            {

                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(Profile));
                System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
                System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(DefaultInfo.Retrieve().profileName + ".profile", FileMode.Open, isf);
                Profile prof = (Profile)xmls.Deserialize(isfs);

                isfs.Close();
                isfs.Dispose();
                return prof;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static List<String> ItemsSource()
        {
            List<String> temp = new List<String>();
            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(Profile));
            using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                String[] names = isf.GetFileNames("*.profile");
                foreach (String str in names)
                {
                    using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream(str, FileMode.Open, isf))
                    {
                        Profile prof = (Profile)xmls.Deserialize(isfs);
                        temp.Add(prof.Name);
                        isfs.Close();
                    }
                }
            }

            return temp;
        }

        public DateTime BirthDate { get; set; }
    }

    public class DefaultInfo
    {
        public String profileName = String.Empty;
       
        public static DefaultInfo Retrieve()
        {
            try
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(DefaultInfo));
                using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("Default.sett", FileMode.OpenOrCreate, isf))
                {
                    DefaultInfo infp = (DefaultInfo)xmls.Deserialize(isfs);
                    isfs.Close();
                    return infp;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public static void Save(DefaultInfo info)
        { 
        System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(DefaultInfo));
        using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
        using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("Default.sett", FileMode.OpenOrCreate, isf))
        {
            xmls.Serialize(isfs, info);
            isfs.Close();
        }
        }
    }

    public class CheckForFirstStart
    {
        public static LevelStore levelsDone = new LevelStore();

        public static bool check()
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists("FirstStart.pfk"))
                    return true;

                else
                    return false;
            }
        }

        public static void CommitFirstStart()
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("FirstStart.pfk", FileMode.CreateNew, isf))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        levelsDone.Store.Add(new Levels() { 
                        Level = "Level " + i.ToString(),
                        LevelNo = i,
                        Data = "NULL"
                        });
                    }

                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(LevelStore));
                    xmls.Serialize(isfs, levelsDone);
                    //StreamReader sr = new StreamReader(isfs);
                    //MessageBox.Show(sr.ReadToEnd());
                    isfs.Flush();
                    isfs.Close();
                }
            }
        }

        public static LevelStore Retrieve()
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("FirstStart.pfk", FileMode.Open, isf))
                {
                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(LevelStore));
                    StreamReader sr = new StreamReader(isfs);                   
                    
                    LevelStore temp = (LevelStore)xmls.Deserialize(sr);

                    isfs.Flush();
                    isfs.Close();
                    return temp;
                }
            }
        }


        public static void MakeChanges(LevelStore Changes)
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFile isf = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new System.IO.IsolatedStorage.IsolatedStorageFileStream("FirstStart.pfk", FileMode.Open, isf))
                {
                    
                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(LevelStore));
                    xmls.Serialize(isfs, Changes);
                    //StreamReader sr = new StreamReader(isfs);                    
                    //MessageBox.Show(sr.ReadToEnd());
                    isfs.Flush(); 
                    isfs.Close();
                    
                }
            }
        }
    }
}
